<?php

namespace App\Http\Middleware;

use App\Services\WxMp\WxMpListService;
use Closure;

/**
 * 检测当前应用是否为开发平台应用
 * @package App\Http\Middleware
 */
class CheckWxOpenPlatform
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $curWxMp = $request->user()->getWxMpCurrent();
        if (empty($curWxMp) || $curWxMp['appid'] == 'system') {
            abort(403, '请选择微信开放平台第三方平台');
        }
        $wxMpListSer = new WxMpListService();
        $curWxMpData = $wxMpListSer->getValidMpByAppid($curWxMp['appid']);
        if ($curWxMpData['type'] != WxMpListService::TYPE_OPEN_PLATFORM) {
            abort(403, '请选择微信开放平台第三方平台');
        }
        if ($curWxMpData['is_enable'] != 1) {
            abort(403, '该微信开放平台应用已停用');
        }
        return $next($request);
    }
}
